package cn.nubia.admin.application.service;

import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.application.bean.SearchGeneral;
import cn.nubia.admin.application.query.GeneralQuery;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by justin on 15/7/14.
 */
@Service
public class SearchGeneralService extends BaseServiceImpl<SearchGeneral>{

    @Autowired
    SystemLogService systemLogService;

    public void saveGeneral(SearchGeneral general){
        general.setBeginTime(DateUtil.getIntegerTime(general.getBeginDate()));
        general.setEndTime(DateUtil.getIntegerTime(general.getEndDate()));
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        Integer curr = DateUtil.getCurrentTime();
        general.setLastModifyTime(curr);
        general.setState(SearchGeneral.STATE_NORMAL);

        if(general.getId() == null) {
            general.setCreateAdminId(adminId);
            general.setCreateAdminName(adminName);
            general.setCreateTime(curr);
            insert(general);
            //log
            systemLogService.log(SystemLog.ModuleTypeEnum.SEARCHGENERALLIST, SystemLog.OperateTypeEnum.ADD, general.getId());
        }else{
            SearchGeneral domain = get(general.getId());
            general.setCreateAdminId(domain.getCreateAdminId());
            general.setCreateAdminName(domain.getCreateAdminName());
            general.setCreateTime(domain.getCreateTime());

            general.setModifyAdminId(adminId);
            general.setModifyAdminName(adminName);
            update(general);
            //log
            systemLogService.log(SystemLog.ModuleTypeEnum.SEARCHGENERALLIST, SystemLog.OperateTypeEnum.EDIT, general.getId());
        }
    }

    public Paged<SearchGeneral> listPagedSearchGeneral(GeneralQuery generalQuery) {
        Query query = Query.build(SearchGeneral.class);
        query.addEq(SearchGeneral.COLUMN_STATE, SearchGeneral.STATE_NORMAL);
        if(generalQuery.getGeneralType()!=null&&generalQuery.getGeneralType()>=0){
            query.addEq(SearchGeneral.COLUMN_GENERALTYPE,generalQuery.getGeneralType());
        }
        if(generalQuery.getGeneralDates()!=null && generalQuery.getGeneralDatee()!=null){
            query.addGt(SearchGeneral.COLUMN_BEGINTIME,DateUtil.getIntegerTime(generalQuery.getGeneralDates())-1);
            query.addLt(SearchGeneral.COLUMN_BEGINTIME,DateUtil.getIntegerTime(generalQuery.getGeneralDatee()));
        }
        query.addOrder(SearchGeneral.COLUMN_BEGINTIME, Query.DBOrder.DESC);
        query.setPaged(generalQuery.getPageNo(), generalQuery.getPageSize());
        Paged<SearchGeneral> searchGeneralPaged=  findPagedByQuery(query);
        for(SearchGeneral sg : searchGeneralPaged.getListData()){
            sg.setBeginTimeShow(DateUtil.getFormatDate(sg.getBeginTime()));
            sg.setEndTimeShow(DateUtil.getFormatDate(sg.getEndTime()));
            sg.setGeneralTypeShow(SearchGeneral.generalTypes.get(String.valueOf(sg.getGeneralType())));
        }
        return searchGeneralPaged;
    }

    public SearchGeneral getSearchGenenralById(Integer id) {
        SearchGeneral sg = get(id);
        sg.setBeginTimeShow(DateUtil.getFormatDate(sg.getBeginTime()));
        sg.setEndTimeShow(DateUtil.getFormatDate(sg.getEndTime()));
        return sg;
    }

    public void deleteSG(Integer id) {
        SearchGeneral sg = get(id);
        sg.setState(SearchGeneral.STATE_DEL);
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        Integer curr = DateUtil.getCurrentTime();
        sg.setModifyAdminName(adminName);
        sg.setModifyAdminId(adminId);
        sg.setLastModifyTime(curr);
        update(sg);
        //log
        systemLogService.log(SystemLog.ModuleTypeEnum.SEARCHGENERALLIST, SystemLog.OperateTypeEnum.DEL, sg.getId());
    }
}
